//Copyright 2011 - Jonathan Allen

using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Specialized;
using Granite.Collections;
using Granite.ComponentModel;

namespace Granite.Eventing
{
	///// <summary>
	///// This is used to attach weak event handlers to the indicated source.
	///// </summary>
	///// <remarks>
	///// If a class encapsulates this then it should also implement INotifyDictionaryChangedWeak.
	///// </remarks>
	//public class DictionaryChangedEventManager
	//{
	//	readonly WeakReferenceCollection<IDictionaryChangedListener> m_Targets = new WeakReferenceCollection<IDictionaryChangedListener>();

	//	bool m_Listening;
	//	readonly INotifyDictionaryChanged m_Source;

	//	/// <summary>
	//	/// Creates a new DictionaryChangedEventManager.
	//	/// </summary>
	//	/// <param name="source"></param>
	//	public DictionaryChangedEventManager(INotifyDictionaryChanged source)
	//	{
	//		m_Source = source;
	//	}

 //       void DictionaryChanged(object sender, DictionaryChangedEventArgs e)
	//	{
	//		m_Targets.CleanUp();

	//		//disconnect if no longer needed
	//		if (m_Targets.Count == 0)
	//		{
	//			m_Listening = false;
	//			m_Source.DictionaryChanged -= DictionaryChanged;
	//		}
	//		else
	//		{
	//			foreach (var target in m_Targets)
	//				target.Invoke(sender, e);
	//		}
	//	}

	//	/// <summary>
	//	/// Adds a weak event handler.
	//	/// </summary>
	//	/// <param name="eventHandler"></param>
	//	public void AddHandler(IDictionaryChangedListener eventHandler)
	//	{
	//		m_Targets.Add(eventHandler);
	//		if (!m_Listening)
	//		{
	//			m_Source.DictionaryChanged += DictionaryChanged;
	//			m_Listening = true;
	//		}
	//	}

	//	/// <summary>
	//	/// Removes a weak event handler.
	//	/// </summary>
	//	/// <param name="eventHandler"></param>
	//	public void RemoveHandler(IDictionaryChangedListener eventHandler)
	//	{
	//		m_Targets.Remove(eventHandler);
	//		m_Targets.CleanUp();

	//		if (m_Targets.Count == 0 && m_Listening)
	//		{
	//			m_Listening = false;
	//			m_Source.DictionaryChanged -= DictionaryChanged;
	//		}
	//	}

	//}
}
